import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lottie/lottie.dart';
import 'package:pet/app/websocket_controller.dart';

class CustomAvatar extends StatelessWidget {
  String avatar;
  double height;
  double width;
  bool showWave;
  CustomAvatar(
      {super.key,
      required this.avatar,
      this.height = 56.0,
      this.width = 56.0,
      this.showWave = true});

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      builder: (WebsocketController controller) {
        return ClipRRect(
          borderRadius: BorderRadius.circular(width),
          child: Stack(
            alignment: AlignmentDirectional.center,
            children: [
              showWave
                  ? controller.isConnect ? SizedBox(
                      height: height + 0,
                      width: width + 0,
                      child: Lottie.asset('assets/lottie/green-wave.json'),
                    ): SizedBox()
                  : SizedBox(),
              Container(
                height: height - 16,
                width: width - 16,
                child: avatar != ''
                    ? ClipRRect(
                        borderRadius: BorderRadius.circular(width),
                        child: CachedNetworkImage(
                          fit: BoxFit.cover,
                          imageUrl: avatar,
                          placeholder: (context, url) =>
                              const CircularProgressIndicator(),
                          errorWidget: (context, url, error) =>
                              const Icon(Icons.error),
                        ),
                      )
                    : const CircleAvatar(
                        backgroundImage: AssetImage(
                          'assets/images/missing-face-logo.png',
                        ),
                      ),
              ),
            ],
          ),
        );
      },
    );
  }
}
